package com.lzh.prometheus.exceptionhandle.event;

import com.lzh.prometheus.exceptionhandle.interfaces.ExceptionNoticeStatisticsRepository;
import com.lzh.prometheus.message.INoticeSendComponent;
import com.lzh.prometheus.pojos.ExceptionNotice;
import com.lzh.prometheus.properties.exception.ExceptionNoticeFrequencyStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;
import java.util.concurrent.Executor;

/**
 * @author zhehen.lu
 * @date 2025/8/15 9:30
 */
public class ExceptionNoticeAsyncSendListener extends AbstractNoticeSendListener {

    private static final Log logger = LogFactory.getLog(ExceptionNoticeAsyncSendListener.class);

    private final Executor executor;

    /**
     * @param exceptionNoticeFrequencyStrategy
     * @param exceptionNoticeStatisticsRepository
     * @param noticeSendComponents
     */
    public ExceptionNoticeAsyncSendListener(ExceptionNoticeFrequencyStrategy exceptionNoticeFrequencyStrategy,
                                            ExceptionNoticeStatisticsRepository exceptionNoticeStatisticsRepository,
                                            List<INoticeSendComponent<ExceptionNotice>> noticeSendComponents, Executor executor) {
        super(exceptionNoticeFrequencyStrategy, exceptionNoticeStatisticsRepository, noticeSendComponents);
        this.executor = executor;
    }

    @Override
    public void onApplicationEvent(ExceptionNoticeEvent event) {
        logger.debug("异步发送消息");
        executor.execute(() -> send(event.getExceptionNotice()));
    }

}
